package com.mygame.game_server.manager;

import com.mygame.game_server.User;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Jim
 * @date 22:41 2022/5/6
 * @description 人员控制
 * @email:huwmz04153277@126.com
 */
public final class UserManager {

    /**
     * 用户字典
     */
    static private final Map<Integer, User> _userMap = new ConcurrentHashMap<>();

    private UserManager() {
    }

    /**
     * 添加用户
     *
     * @param u
     */
    public static void addUser(User u) {
        if (null != u) {
            _userMap.putIfAbsent(u.userId, u);
        }
    }

    /**
     * 移除用户
     *
     * @param userId
     */
    public static void removeUser(int userId) {
        _userMap.remove(userId);
    }

    /**
     * 用户列表
     *
     * @return
     */
    public static Collection<User> listUser() {
        return _userMap.values();
    }

    /**
     * 更具用户ID获取user
     * @param userId
     * @return
     */
    public static User getUserById(int userId) {
        return _userMap.get(userId);
    }
}
